﻿Shader "Custom/Terrain4" {
	Properties {
		 _Control ("Control (RGBA)", 2D) = "red" {}
		 _Splat3 ("Layer 3 (A)", 2D) = "white" {}
		 _Splat2 ("Layer 2 (B)", 2D) = "white" {}
		 _Splat1 ("Layer 1 (G)", 2D) = "white" {}
		 _Splat0 ("Layer 0 (R)", 2D) = "white" {}
	}

	Category {
		Tags {
			"Queue" = "Geometry-99"
			"IgnoreProjector"="True"
			"RenderType" = "Opaque"
		}


		SubShader { 
			pass {
				Name "FORWARD"
				Tags { "LightMode" = "ForwardBase" }
				Lighting Off

				CGPROGRAM
				#pragma vertex vert_surf
				#pragma fragment frag_surf
				#pragma multi_compile_fwdbase

				#include "UnityCG.cginc"
				#include "Lighting.cginc"

				#include "TerrainSplatmapCommon.cginc"
				sampler2D _FuckTex;

				struct v2f_surf {
			        float4 pos : SV_POSITION;
			  		float4 pack0 : TEXCOORD0; // _Splat0 _Splat1
			  		float4 pack1 : TEXCOORD1; // _Splat2 _Splat3
			  		float2 custompack0 : TEXCOORD4;
			    };

			    struct SurfaceOutput2 {
					fixed3 Albedo;
					fixed Alpha;
				};

			    float4 _Splat0_ST;
				float4 _Splat1_ST;
				float4 _Splat2_ST;
				float4 _Splat3_ST;

				v2f_surf vert_surf (appdata_full v) {
				  v2f_surf o;
				  UNITY_INITIALIZE_OUTPUT(v2f_surf,o);
				  Input customInputData;
				  SplatmapVert (v, customInputData);
				  o.custompack0.xy = customInputData.tc_Control;
				  o.pos = UnityObjectToClipPos(v.vertex);
				  o.pack0.xy = TRANSFORM_TEX(v.texcoord, _Splat0);
				  o.pack0.zw = TRANSFORM_TEX(v.texcoord, _Splat1);
				  o.pack1.xy = TRANSFORM_TEX(v.texcoord, _Splat2);
				  o.pack1.zw = TRANSFORM_TEX(v.texcoord, _Splat3);
				  return o;
				}


				fixed4 splatAlphaBlend(fixed4 current, sampler2D img, fixed2 uv, fixed alpha) {
					fixed4 imgCol = tex2D(img, uv);
					//fixed realAlpha = alpha * imgCol.a;
					fixed realAlpha = alpha;
					//realAlpha = clamp(realAlpha, 0.1, 1);
						//realAlpha = sin(realAlpha*1.570);
					//realAlpha = smoothstep(0.3, 1, realAlpha);
					//realAlpha = step(0.3, realAlpha);
					fixed4 baseCol = (1-realAlpha) * current  + realAlpha * imgCol ;
					return baseCol;
					//return imgCol;
				}


				fixed4 alphaOutline(fixed alpha, fixed4 baseColor, fixed4 outlineColor, fixed4 current) {
					//0.8 ~ 0.9  0.8~0.82   lerp
					//alpha 1-0.8 edge  0-1 ---> 1-0
					// baseColor.a 
					const fixed low = 0.155;
					const fixed lowUp = 0.8;
					const fixed hiLow = 0.85;
					const fixed hi = 0.2;
					const fixed bl = 0.5;

					fixed factor = step(low, alpha) * step(alpha, hi);
					/*
					fixed whichInt = step(alpha, lowUp); 
					fixed4 sc1 = lerp(baseColor, outlineColor, smoothstep(low, lowUp, alpha)) * whichInt;
					fixed4 sc2 = lerp(outlineColor, baseColor, smoothstep(hiLow, hi, alpha)) * (1-whichInt);
					*/
					//return factor * (sc1 + sc2) + (1-factor) * baseColor;
					//alpha = smoothstep(0.3, 1, alpha);
					alpha = step(low, alpha);
					return factor * ((1-bl)*current + outlineColor) + (1-factor) *((1-alpha)*current + alpha * baseColor ) ;
				}

				fixed4 splatOutlineBlend(fixed4 current, sampler2D decalMap, fixed2 uv, fixed alpha) {
					const fixed4 outlineColor = fixed4(0, 0, 0, 1); 

					fixed4 col = tex2D(decalMap, uv);
					fixed4 outlined = alphaOutline(alpha, col, outlineColor, current);
					//return (1-alpha*outlined.a) * current + alpha * outlined;
					return outlined;
				}

				void SplatmapMix2(Input IN, out half4 splat_control, out fixed4 mixedDiffuse)
				{
					splat_control = tex2D(_Control, IN.tc_Control);

					fixed4 mainCol = tex2D(_FuckTex, IN.tc_Control);
					mixedDiffuse = mainCol;

					/*
					mixedDiffuse = splatAlphaBlend(mixedDiffuse, _Splat0, IN.uv_Splat0, splat_control.r);
					mixedDiffuse = splatAlphaBlend(mixedDiffuse, _Splat1, IN.uv_Splat1, splat_control.g);
					mixedDiffuse = splatAlphaBlend(mixedDiffuse, _Splat2, IN.uv_Splat2, splat_control.b);
					mixedDiffuse = splatAlphaBlend(mixedDiffuse, _Splat3, IN.uv_Splat3, splat_control.a);
					*/

					mixedDiffuse = splatAlphaBlend(mixedDiffuse, _Splat0, IN.uv_Splat0, splat_control.r);
					mixedDiffuse = splatAlphaBlend(mixedDiffuse, _Splat1, IN.uv_Splat1, splat_control.g);
					mixedDiffuse = splatOutlineBlend(mixedDiffuse, _Splat2, IN.uv_Splat2, splat_control.b);
					mixedDiffuse = splatOutlineBlend(mixedDiffuse, _Splat3, IN.uv_Splat3, splat_control.a);

				}
				void surf(Input IN, inout SurfaceOutput2 o)
				{
					half4 splat_control;
					fixed4 mixedDiffuse;
					SplatmapMix2(IN, splat_control, mixedDiffuse);

					o.Albedo = mixedDiffuse.rgb;
					o.Alpha = 1;
				}

				fixed4 frag_surf (v2f_surf IN) : SV_Target {
				  // prepare and unpack data
				  Input surfIN;
				  UNITY_INITIALIZE_OUTPUT(Input,surfIN);
				  surfIN.uv_Splat0.x = 1.0;
				  surfIN.uv_Splat1.x = 1.0;
				  surfIN.uv_Splat2.x = 1.0;
				  surfIN.uv_Splat3.x = 1.0;
				  surfIN.tc_Control.x = 1.0;
				  surfIN.uv_Splat0 = IN.pack0.xy;
				  surfIN.uv_Splat1 = IN.pack0.zw;
				  surfIN.uv_Splat2 = IN.pack1.xy;
				  surfIN.uv_Splat3 = IN.pack1.zw;
				  surfIN.tc_Control = IN.custompack0.xy;
			      fixed3 lightDir = _WorldSpaceLightPos0.xyz;

				  SurfaceOutput2 o = (SurfaceOutput2)0;

				  o.Albedo = 0.0;
				  o.Alpha = 0.0;

				  surf (surfIN, o);
				  fixed4 c = fixed4(o.Albedo, o.Alpha);

				  return c;
				}
				ENDCG
			}

		}

	}

}
